package br.com.sgi.util;



import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.Date;

import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

import br.com.sgi.exception.SgiException;
import br.com.sgi.util.email.EnviaEmail;

import com.sun.faces.application.ActionListenerImpl;

public class ExceptionJsfListener extends ActionListenerImpl implements ActionListener, Serializable
{

   /** A constante serialVersionUID. */
   private static final long serialVersionUID = 4122411584718772627L;

   /** target view id. */
   private String targetViewId = null;
   
   /** redirecting. */
   private boolean redirecting = false;
   
   /** outcome. */
   private String outcome = "/erro/erro";
   
   /** A constante MENSAGEM_ERRO. */
   private static final String MENSAGEM_ERRO = "Ocorreu um erro inesperado no sistema. O administrador do sistema será notificado.";

   /* (non-Javadoc)
    * @see com.sun.faces.application.ActionListenerImpl#processAction(javax.faces.event.ActionEvent)
    */
   @Override
   public void processAction(ActionEvent event)
   {
      try{
         super.processAction(event);
      }catch (Exception e) {
         
    	
         
         FacesContext facesContext = FacesContext.getCurrentInstance();

         FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, MENSAGEM_ERRO,MENSAGEM_ERRO);
         facesContext.addMessage(null, fm);
         
         ExternalContext externalContext = facesContext.getExternalContext();
         
         ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
         
         redirecting = FacesUtils.isRedirect(outcome);
         targetViewId = FacesUtils.getTargetViewId(facesContext, outcome);
         
         if (redirecting) {
            String redirectPath = viewHandler.getActionURL(facesContext, targetViewId);

            try {
               externalContext.redirect(externalContext.encodeActionURL(redirectPath));
            } catch (IOException ioe) {
               throw new FacesException(e.getMessage(), ioe);
            }
         } else {
            try {
               if (facesContext.getExternalContext().getResource(targetViewId) == null) {
                  return;
               }
            } catch (MalformedURLException mal) {
               throw new SgiException(mal);
            }
            UIViewRoot viewRoot = viewHandler.createView(facesContext, targetViewId);
            facesContext.setViewRoot(viewRoot);
            facesContext.renderResponse();
            e.printStackTrace();
         }
         
         try {
        	 EnviaEmail email = new EnviaEmail();
        	 email.setAssunto("Erro " + new Date());
        	 email.setMensagem(e.toString());
        	 email.setDe(Constante.EMAIL);
        	 email.setPara(Constante.EMAIL_CONTATO);
        	 email.enviar();
		} catch (Exception e2) {
		}
        
         
      }
   }
}
